package com.justech.eat.service.impl;

import com.justech.common.util.DateUtil;
import com.justech.common.vo.PageDto;
import com.justech.eat.controller.req.OrderDefaultSetReq;
import com.justech.eat.controller.req.OrderDetailReq;
import com.justech.eat.entity.Order;
import com.justech.eat.entity.OrderDefaultSet;
import com.justech.eat.entity.OrderDetailAbnormalRecord;
import com.justech.eat.mapper.mysql.OrderDetailMapper;
import com.justech.eat.repository.OrderDefaultSetRepository;
import com.justech.eat.repository.OrderRepository;
import com.justech.eat.service.OrderBook;
import com.justech.eat.service.UserOrientedOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: eat-service
 * @description:
 * @author: feifei.li
 * @create: 2020-05-28 14:13
 **/
@Primary
@Service
public class UserOrientedOrderServiceImpl implements UserOrientedOrderService {

    @Autowired
    OrderRepository orderRepository;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Autowired
    OrderDefaultSetRepository orderDefaultSetRepository;


    /**
     * 查询员工当前月的报餐明细
     *
     * @param jobNumber
     * @return
     */
    @Override
    public List<Order> thisMonthOrders(String jobNumber) {
        return orderRepository.findByDateBetweenAndJobNumber(getFirstDayByCurrentMonth(), getLastDayByCurrentMonth(), jobNumber);
    }

    private String getFirstDayByCurrentMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, 0);
        cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        return format.format(cal_1.getTime());
    }

    private String getLastDayByCurrentMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        return format.format(ca.getTime());
    }

    private String prevMonthFirst() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        Date time = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(time);
    }

    private String prevMonthLast() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        Date time = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(time);
    }

    /**
     * 查询用户上个月的报餐明细
     *
     * @param jobNumber
     * @return
     */
    @Override
    public List<Order> previousMonthOrders(String jobNumber) {
        return orderRepository.findByDateBetweenAndJobNumber(jobNumber, prevMonthFirst(), prevMonthLast());
    }

    /**
     * 获取某个用户区间内的报餐明细
     *
     * @param s         开始日期
     * @param e         结束日期
     * @param jobNumber 用户工号
     * @return
     */
    @Override
    public List<Order> getMonthOrders(String s, String e, String jobNumber) {
        return orderRepository.findByDateBetweenAndJobNumber(s, e, jobNumber);
    }

    @Qualifier(OrderBook.order_book_save)
    @Autowired
    OrderBook orderBook;

    /**
     * 提交一日报餐信息
     *
     * @param orderDetailReq
     * @param jobNumber
     * @param date
     * @return
     */
    @Override
    public List<Order> book(OrderDetailReq orderDetailReq, String date, String jobNumber) {
        orderBook.submit(orderDetailReq, date, jobNumber);
        return null;
    }


    /**
     * 设置默认报餐信息
     *
     * @param orderDefaultSetReq
     * @param jobNumber
     * @return
     */
    @Override
    public OrderDefaultSet defaultSet(OrderDefaultSetReq orderDefaultSetReq, String jobNumber) {
        OrderDefaultSet defaultSet = orderDefaultSetRepository.findByJobNumber(jobNumber);
        if (defaultSet == null) {
            defaultSet = new OrderDefaultSet();
            defaultSet.setCreateTime(DateUtil.now());
        } else {
            defaultSet.setUpdateTime(DateUtil.now());
        }
        defaultSet.setLunch(orderDefaultSetReq.getLunch());
        defaultSet.setDinner(orderDefaultSetReq.getDinner());
        defaultSet.setMeal(orderDefaultSetReq.getMeal());
        defaultSet.setNight(orderDefaultSetReq.getNight());
        defaultSet.setJobNumber(jobNumber);
        return orderDefaultSetRepository.saveAndFlush(defaultSet);
    }

    /**
     * 查询用户的异常信息
     *
     * @param jobNumber
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDto<OrderDetailAbnormalRecord> getAbnormalRecord(String jobNumber, Integer page, Integer size) {
        return null;
    }

    /**
     * 查询用户的异常信息
     *
     * @param jobNumber
     * @param date
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDto<OrderDetailAbnormalRecord> getAbnormalRecordByDate(String jobNumber, String date, Integer page, Integer size) {
        return null;
    }
}
